package ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import ssm.entity.Student;
import ssm.service.StudentService;

@Controller
public class StudentControl {
	
	@Autowired
	private StudentService studentService;
	
	
	@RequestMapping(value="/allstudent",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getStudentByPageNumber(HttpServletRequest request) {
		
		Student pageStudent = new Student();
		//获取第几页
		int page = Integer.parseInt(request.getParameter("page"));
		//获取每页数据条数
		int limit = Integer.parseInt(request.getParameter("limit"));
		
		int startPageNumber = (page-1)*limit;
		int endPageNumber = limit*page;
		
		pageStudent.setStartPageNumber(startPageNumber);
		pageStudent.setEndPageNumber(endPageNumber);
		//分页查询学生
		List<Student> list = studentService.getStudentByPageNumber(pageStudent);
		//查询学生总数量
		List<Student> allStudentList = studentService.getAllStudent();
		return "{\"code\":0,\"count\":"+allStudentList.size()+",\"data\":"+JSON.toJSONString(list)+"}";
	}
	
	@RequestMapping(value="/deletestudent",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deleteStudent(int sId) {
		int result = studentService.deleteStudent(sId);
		if(result>0) {
			return "1";
		}else {
			return "0";
		}
	}
	
	@RequestMapping(value="/batchdeletestudent",produces="text/html;charset=UTF-8;application/json")
	@ResponseBody
	public String batchDeleteStudent(List<Student> list) {
		for(int i = 0;i <= list.size(); i++) {
			
		}
		System.out.println(list.get(0).getsId());
		return "";
	}
	
}
